/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool.preference;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

public class Preferences {
    private static final String TAG = "Preferences";

    private SharedPreferences mSp;

    /**
     * 构建 Preferences 实例。
     *
     * @param context Context
     * @param cfgName 配置文件名称
     * @return Shared preference.
     */
    public static Preferences build(Context context, String cfgName) {
        return new Preferences(context, cfgName);
    }

    /**
     * 使用默认名称 构建 Preferences 实例.
     *
     * @param context context
     * @return Preferences
     */
    public static Preferences build(Context context) {
        return new Preferences(context,
                context.getApplicationContext().getPackageName() + ".common");
    }

    private Preferences() {
    }

    private Preferences(Context context, String cfgName) {
        if (null == context) {
            throw new IllegalArgumentException(TAG + " constructor context is null");
        }
        if (null == cfgName || cfgName.isEmpty()) {
            throw new IllegalArgumentException(TAG + " constructor cfgName is empty");
        }
        mSp = context.getApplicationContext().getSharedPreferences(cfgName, Context.MODE_PRIVATE);
    }

    /**
     * contains
     *
     * @param key key
     * @return bool
     */
    public boolean contains(String key) {
        return mSp.contains(key);
    }

    /**
     * get all
     *
     * @return map String
     */
    public Map<String, ?> getAll() {
        return mSp.getAll();
    }

    /**
     * clear data
     *
     * @return bool
     */
    public boolean clear() {
        return mSp.edit().clear().commit();
    }

    /**
     * get int
     *
     * @param key      key
     * @param defValue value
     * @return int
     */
    public int getInt(String key, int defValue) {
        return mSp.getInt(key, defValue);
    }

    /**
     * get string
     *
     * @param key      key
     * @param defValue value
     * @return String
     */
    public String getString(String key, String defValue) {
        return mSp.getString(key, defValue);
    }

    /**
     * get boolean
     *
     * @param key      key
     * @param defValue value
     * @return bool
     */
    public boolean getBoolean(String key, boolean defValue) {
        return mSp.getBoolean(key, defValue);
    }

    /**
     * get float
     *
     * @param key      key
     * @param defValue value
     * @return float
     */
    public float getFloat(String key, float defValue) {
        return mSp.getFloat(key, defValue);
    }

    /**
     * put long
     *
     * @param key      key
     * @param defValue value
     * @return long
     */
    public long getLong(String key, long defValue) {
        return mSp.getLong(key, defValue);
    }

    /**
     * put int
     *
     * @param key   key
     * @param value value
     * @return bool
     */
    public boolean putInt(String key, int value) {
        return mSp.edit().putInt(key, value).commit();
    }

    /**
     * put long
     *
     * @param key   key
     * @param value value
     * @return bool
     */
    public boolean putLong(String key, long value) {
        return mSp.edit().putLong(key, value).commit();
    }

    /**
     * put boolean
     *
     * @param key   key
     * @param value value
     * @return bool
     */
    public boolean putBoolean(String key, boolean value) {
        return mSp.edit().putBoolean(key, value).commit();
    }

    /**
     * put float
     *
     * @param key   key
     * @param value value
     * @return bool
     */
    public boolean putFloat(String key, float value) {
        return mSp.edit().putFloat(key, value).commit();
    }

    /**
     * put string
     *
     * @param key   key
     * @param value value
     * @return bool
     */
    public boolean putString(String key, String value) {
        return mSp.edit().putString(key, value).commit();
    }

    /**
     * remove key
     *
     * @param key key
     * @return boolean
     */
    public boolean removeKey(String key) {
        return mSp.edit().remove(key).commit();
    }
}
